package com.ruoyi.rtsp.util;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.sign.Base64;
import org.apache.commons.lang3.ArrayUtils;

import javax.websocket.EncodeException;
import javax.websocket.Encoder;
import javax.websocket.EndpointConfig;

/**
 * 图片转码器
 */
public class ImageEncoder implements Encoder.Text<Image> {
 
    @Override
    public String encode(Image image) throws EncodeException {
        if(image != null && !ArrayUtils.isEmpty(image.getImageByte())){
            String base64Image= Base64.encode(image.getImageByte());
            return JSON.toJSONString(new AjaxResult(AjaxResult.Type.SUCCESS,"获取帧成功",base64Image));
        }
        return JSON.toJSONString(new AjaxResult(AjaxResult.Type.ERROR,"获取帧失败",null));
    }
 
    @Override
    public void init(EndpointConfig endpointConfig) {
 
    }
 
    @Override
    public void destroy() {
 
    }
}